import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.safewind.wx.bean.Button;
import com.safewind.wx.bean.CommonButton;
import com.safewind.wx.bean.ComplexButton;
import com.safewind.wx.bean.Menu;
import com.safewind.wx.bean.ViewButton;
import com.safewind.wx.constant.WxConstant;
import com.safewind.wx.util.WxUtil;

public class GenerateMenu {
    private static final Logger logger = LoggerFactory.getLogger(WxUtil.class);

    public static void main(String[] args) { // 调用接口获取access_token
        int result = WxUtil.createMenu(getMenu());
        if (0 == result) {
            logger.info("菜单创建成功！");
        } else {
            logger.info("菜单创建失败，错误码：" + result);
        }
    }

    /**
     * 组装菜单数据
     *
     * @return
     */
    private static Menu getMenu() {

        ViewButton btn11 = getViewBtn("mes", "发送消息");
        ViewButton btn21 = getViewBtn("address", "查看通讯录");
        ViewButton btn32 = getViewBtn("help", "帮助");
        ViewButton btn33 = getViewBtn("logout", "解绑");

        CommonButton btn34 = new CommonButton();
        btn34.setName("help");
        btn34.setKey("help");
        btn34.setType("click");

        ComplexButton mainBtn2 = new ComplexButton();
        mainBtn2.setName("帮助");
        mainBtn2.setSub_button(new Button[] { btn32, btn33, btn34 });

        Menu menu = new Menu();
        menu.setButton(new Button[] { btn11, btn21, mainBtn2 });

        return menu;
    }

    private static ViewButton getViewBtn(String arg0, String arg1) {
        String url = WxConstant.AUTH_URL;
        url = url.replace("APPID", WxConstant.APP_ID).replace("REDIRECTURI", WxConstant.WEB_URL + "/wx/" + arg0);
        ViewButton btn = new ViewButton();
        btn.setName(arg1);
        btn.setType("view");
        btn.setUrl(url);
        return btn;
    }
}
